package persistence;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;

import entity.Cliente;

public class ClienteDao {
	
	Session session;
	Transaction transaction;
	Criteria criteria;
	Query query;
	
	public void cadastrarCli(Cliente c) throws Exception {
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		session.save(c);
		transaction.commit();
		session.close();		
	}
	
	public List<Cliente> listarCli() throws Exception {
		session = HibernateUtil.getSessionFactory().openSession();
		List<Cliente> listaCli = new ArrayList<Cliente>();
		criteria = session.createCriteria(Cliente.class);
		criteria.addOrder(Order.asc("nomeCliente"));
		listaCli = criteria.list();
		session.close();
		return listaCli;
	}
	
	public void editarCli(Cliente c) throws Exception {
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		session.update(c);
		transaction.commit();
		session.close();
	}
	
	public void excluirCli(Cliente c) throws Exception {
		session = HibernateUtil.getSessionFactory().openSession();
		transaction = session.beginTransaction();
		session.delete(c);
		transaction.commit();
		session.close();
	}

}
